﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using ScormModel;
using ScormModel.View;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using ScormModel.HtmlBuilder;
using System.Collections.ObjectModel;
namespace ScormModel.Question
{
    public class QuestionTestData
    {
        ObservableCollection<QuestionTest> listQuestions;
        List<Objective> listObj;
        string numOfCorrectAnswer;
        string targetObj ="";
        public ObservableCollection<QuestionTest> ListQuestions
        {
            get { return this.listQuestions; }
            set { this.listQuestions = value; }
        }
        public List<Objective> ListObj
        {
            get { return this.listObj; }
            set { this.listObj = value; }
        }
        public string NumOfCorrectAnswer
        {
            get { return this.numOfCorrectAnswer; }
            set { this.numOfCorrectAnswer = value; }
        }
        public string TargetObj
        {
            get { return this.targetObj; }
            set { this.targetObj = value; }
        }
        public HtmlDocument createTest()
        {
            StreamReader sr = new StreamReader(Directory.GetCurrentDirectory() + "\\QuestionTemplates\\" + "QuestionTemplatePage.htm", System.Text.UnicodeEncoding.UTF8);
            string readHtmlCode = sr.ReadToEnd();
            sr.Close();
            ScormModel.HtmlBuilder.HtmlDocument mDocument = ScormModel.HtmlBuilder.HtmlDocument.Create(readHtmlCode, true);//sr.ReadToEnd(),true);

            ScormModel.HtmlBuilder.HtmlElement htmlElement = (ScormModel.HtmlBuilder.HtmlElement)mDocument.Nodes["HTML"];
            ScormModel.HtmlBuilder.HtmlElement questionElement = (ScormModel.HtmlBuilder.HtmlElement)htmlElement.Nodes["BODY"];
            string javascriptCode = "";
            for (int i = 0; i < ListQuestions.Count; i++)
            {
                string jscode = "";
                ScormModel.HtmlBuilder.HtmlElement inputForm = new ScormModel.HtmlBuilder.HtmlElement("form");
                inputForm.Nodes.Clear();
                ListQuestions[i].QuestionData.createQuestion(inputForm,i);
                questionElement.Nodes.Add(inputForm);
                jscode = ListQuestions[i].QuestionData.CreateJavaScriptForTest();
                javascriptCode += jscode;
                ScormModel.HtmlBuilder.HtmlElement brtext = new ScormModel.HtmlBuilder.HtmlElement("br");
                questionElement.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)brtext);
            }
            ScormModel.HtmlBuilder.HtmlElement scriptElement = new ScormModel.HtmlBuilder.HtmlElement("script");
            HtmlAttribute languageAttribute = new HtmlAttribute("language", "javascript");
            HtmlText javaScriptCode = new HtmlText(CreateJavaScriptHeader() + javascriptCode + createJavaScriptFooter());
            scriptElement.Attributes.Add(languageAttribute);
            scriptElement.Nodes.Add((HtmlNode)javaScriptCode);

            questionElement.Nodes.Add((HtmlNode)scriptElement);
            questionElement.Nodes.Add((HtmlNode)this.WriteSubmitButtonHtmlCode());
            return mDocument;
        }
        public HtmlDocument createCompetencyTest()
        {
            StreamReader sr = new StreamReader(Directory.GetCurrentDirectory() + "\\QuestionTemplates\\" + "QuestionTemplatePage.htm", System.Text.UnicodeEncoding.UTF8);
            string readHtmlCode = sr.ReadToEnd();
            sr.Close();
            ScormModel.HtmlBuilder.HtmlDocument mDocument = ScormModel.HtmlBuilder.HtmlDocument.Create(readHtmlCode, true);//sr.ReadToEnd(),true);

            ScormModel.HtmlBuilder.HtmlElement htmlElement = (ScormModel.HtmlBuilder.HtmlElement)mDocument.Nodes["HTML"];
            ScormModel.HtmlBuilder.HtmlElement questionElement = (ScormModel.HtmlBuilder.HtmlElement)htmlElement.Nodes["BODY"];
            string javascriptCode = "";
            for (int i = 0; i < ListQuestions.Count; i++)
            {
                string jscode = "";
                ScormModel.HtmlBuilder.HtmlElement inputForm = new ScormModel.HtmlBuilder.HtmlElement("form");
                inputForm.Nodes.Clear();
                ListQuestions[i].QuestionData.createQuestion(inputForm, i);
                questionElement.Nodes.Add(inputForm);
                jscode = ListQuestions[i].QuestionData.CreateJavaScriptForTest();
                javascriptCode += jscode;
                ScormModel.HtmlBuilder.HtmlElement brtext = new ScormModel.HtmlBuilder.HtmlElement("br");
                questionElement.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)brtext);
            }
            ScormModel.HtmlBuilder.HtmlElement scriptElement = new ScormModel.HtmlBuilder.HtmlElement("script");
            HtmlAttribute languageAttribute = new HtmlAttribute("language", "javascript");
            HtmlText javaScriptCode = new HtmlText(CreateCompetencyJavaScriptHeader() + javascriptCode + createCompetencyJavaScriptFooter());
            scriptElement.Attributes.Add(languageAttribute);
            scriptElement.Nodes.Add((HtmlNode)javaScriptCode);

            questionElement.Nodes.Add((HtmlNode)scriptElement);
            questionElement.Nodes.Add((HtmlNode)this.WriteSubmitButtonHtmlCode());
            return mDocument;
        }
        
        public string CreateQuestionCheckPoint()
        {
            string temp = "";
            return temp;
        }
        public string CreateCompetencyJavaScriptHeader()
        {
            string temp = "";
            for (int i = 0; i < ListObj.Count; i++)
            {
                temp += "\nvar setStatus" + (i + 1).ToString() + "= 0;";
            }
            temp += "\nvar exitPageStatus;\nvar totalScore;\nvar totalQuestion;\nvar result;\n";
            temp += "function sumitvalue()\n{\ncalcScore();\nexitPageStatus=true;\ndoTerminate();\n}";
            temp += "function loadPage()\n{\ndoInitialize();\nexitPageStatus = false;\n}\n";
            temp += "function unloadPage()\n{\nif(exitPageStatus != true)\n{\nsumitvalue();\n}\n}\n";
            temp += "\nfunction findObjective(obj)\n{\nvar numOfObj = doGetValue(\"cmi.objectives._count\");\nvar objectiveLocation = -1; ";
            temp += "\nfor ( var i=0; i < numOfObj; i++ ) \n";
            temp += "{ \nif ( doGetValue(\"cmi.objectives.\" + i + \".id\") == obj )\n{\nobjectiveLocation = i;\nbreak;\n}\n}\n";
            temp += "\nif(objectiveLocation == -1 )\n{\n alert(\"objective not found\");\nobjectiveLocation = numOfObj;\ndoSetValue( \"cmi.objectives.\" + objectiveLocation + \".id\", obj);\n}";
            temp += "\nreturn objectiveLocation;";
            temp += "\n}\n";
            temp += "\nfunction setObjToPassed(index)\n{\ndoSetValue(\"cmi.objectives.\" + index + \".success_status\",\"passed\");\n}";
            temp += "\nfunction setObjToFailed(index)\n{\ndoSetValue(\"cmi.objectives.\" + index + \".success_status\",\"failed\");\n}";
            temp += "\nfunction calcScore()\n{\ndocument.all.submitButton.disabled=true;\ntotalScore=0;\ntotalQuestion = 0;\n";
            return temp;
        }
        public string CreateJavaScriptHeader()
        {
            string temp = "";
            temp += "\nvar exitPageStatus;\nvar totalScore;\nvar totalQuestion;\nvar result;\n";
            temp += "function sumitvalue()\n{\ncalcScore();\nexitPageStatus=true;\ndoTerminate();\n}";
            temp += "function loadPage()\n{\ndoInitialize();\nexitPageStatus = false;\n}\n";
            temp += "function unloadPage()\n{\nif(exitPageStatus != true)\n{\nsumitvalue();\n}\n}\n";
            temp += "\nfunction findObjective(obj)\n{\nvar numOfObj = doGetValue(\"cmi.objectives._count\");\nvar objectiveLocation = -1; ";
            temp += "\nfor ( var i=0; i < numOfObj; i++ ) \n";
            temp += "{ \nif ( doGetValue(\"cmi.objectives.\" + i + \".id\") == obj )\n{\nobjectiveLocation = i;\nbreak;\n}\n}\n";
            temp += "\nif(objectiveLocation == -1 )\n{\nalert(\"objective not found\");\nobjectiveLocation = numOfObj;\ndoSetValue( \"cmi.objectives.\" + objectiveLocation + \".id\", obj);\n}";
            temp += "\nreturn objectiveLocation;";
            temp += "\n}\n";
            temp += "\nfunction setObjToPassed(index)\n{\ndoSetValue(\"cmi.objectives.\" + index + \".success_status\",\"passed\");\n}";
            temp += "\nfunction setObjToFailed(index)\n{\ndoSetValue(\"cmi.objectives.\" + index + \".success_status\",\"failed\");\n}";
            temp += "function calcScore()\n{\ndocument.all.submitButton.disabled=true;\ntotalScore=0;\ntotalQuestion = 0;\n";
            return temp;
        }
        public string createCompetencyJavaScriptFooter()
        {
            string temp = "\nvar target;";
            temp += "\nif(totalScore/totalQuestion >= 0.9)\n{ \n";
            for (int i = 0; i < ListObj.Count; i++)
            {
                temp += "\ntarget = findObjective(\"" + ListObj[i].ObjectiveID + "\");";
                temp += "\nsetObjToPassed(target );";
            }
            temp += "}\n";
            temp += "\nelse if(totalScore/totalQuestion < 0.9 && totalScore/totalQuestion >= 0.8)\n{ \n";
            for (int i = 0; i < ListObj.Count - 1; i++)
            {
                temp += "\ntarget = findObjective(\"" + ListObj[i].ObjectiveID + "\");";
                temp += "\nsetObjToPassed(target );";
            }
            temp += "\ntarget = findObjective(\"" + ListObj[ListObj.Count - 1].ObjectiveID + "\");";
            temp += "\nsetObjToFailed(target );";
            temp += "}\n";
            temp += "else{\n";
            for (int i = 0; i < ListObj.Count; i++)
            {
                temp += "\ntarget = findObjective(\"" + ListObj[i].ObjectiveID + "\");";
                temp += "\nsetObjToFailed(target );";
            }
            temp += "}\n";
            temp += "}\n";
            return temp;
        }
        public string createJavaScriptFooter()
        {
            string temp = "";
            temp += "if(totalScore>=" + NumOfCorrectAnswer + ")\n{\ndoSetValue(\"cmi.success_status\",\"passed\");\n";
            if (TargetObj.Length > 0)
            {
                temp += "\n var tempIndex = findObjective(\"" + TargetObj + "\")\n";
                temp += "\nsetObjToPassed(tempIndex)\n";
            }
            temp += "doSetValue(\"cmi.score.scaled\",totalScore/totalQuestion);\n}\n";
            temp += "else\n{\ndoSetValue(\"cmi.success_status\",\"failed\");\n";
            if (TargetObj.Length > 0)
            {
                temp += "\n var tempIndex = findObjective(\"" + TargetObj + "\")\n";
                temp += "\nsetObjToFailed(tempIndex)\n";
            }
            temp += "doSetValue(\"cmi.score.scaled\",totalScore/totalQuestion);\n}\n";
            temp += "}\n";
            return temp;
        }
        private ScormModel.HtmlBuilder.HtmlElement WriteSubmitButtonHtmlCode()
        {
            //write submit button html code
            ScormModel.HtmlBuilder.HtmlElement inputForm = new ScormModel.HtmlBuilder.HtmlElement("form");
            int temp = inputForm.Nodes.Add((HtmlNode)new ScormModel.HtmlBuilder.HtmlElement("p"));
            ScormModel.HtmlBuilder.HtmlElement inputElement = new ScormModel.HtmlBuilder.HtmlElement("input");
            HtmlAttribute typeAttribute = new HtmlAttribute("type", "button");
            HtmlAttribute nameAttribute = new HtmlAttribute("name", "submitButton");
            HtmlAttribute valueAttribute = new HtmlAttribute("value", " Submit ");
            HtmlAttribute onClickAttribute = new HtmlAttribute("onClick", "calcScore()");
            inputElement.Attributes.Add(typeAttribute);
            inputElement.Attributes.Add(nameAttribute);
            inputElement.Attributes.Add(valueAttribute);
            inputElement.Attributes.Add(onClickAttribute);
            ((ScormModel.HtmlBuilder.HtmlElement)inputForm.Nodes[temp]).Nodes.Add((HtmlNode)inputElement);
            return inputElement;
        }
    }
}
